package com.mx.fs;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class FileMeta implements Serializable {

	private List<MetaItem> metaList = new ArrayList<MetaItem>();
	
	//The size after serialize
	private int objectSize;

	public FileMeta() {

	}

	public FileMeta(MetaItem... items) {
		for (int i = 0; i < items.length; i++) {
			metaList.add(items[i]);
		}
	}

	public List<MetaItem> getMetaList() {
		return metaList;
	}

	public void addMetaItem(MetaItem item) {
		metaList.add(item);
	}

	public int getObjectSize(){
		return objectSize;
	}
	
	public byte[] toByte() {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			ObjectOutputStream oos = new ObjectOutputStream(out);
			oos.writeObject(this);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] b = out.toByteArray();
		objectSize = b.length;
		return b;
	}
	
	public ByteBuffer toByteBuffer(){
		byte[] b = toByte();
		ByteBuffer buffer = ByteBuffer.allocate(b.length);
		buffer.put(b);
		buffer.flip();
		return buffer;
	}
	
	public String toString(){
		return metaList.toString();
	}
	
	
}
